/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFUnpacker
  \brief the abstract base class for unpacking of the raw data from Daq.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-09-27
  \date 2021-09-27 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date        <th>Author      <th>Description                    </tr>
  <tr>  <td>2021-09-27   <td>Asia Sun    <td>file created                   </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <iostream>
#include <TClonesArray.h>
#include "ETFRun.h"
#include "ETFUnpacker.h"
#include "ETFReadShm.h"
#include "ETFMsg.h"
#include "ETFParaManager.h"

using std::cout;
using std::endl;

ETFUnpacker::ETFUnpacker(DaqType type, int id) : fType(type), fId(id),
fCurrentEventIndex(0), fEventCnt(0), fSizeCnt(0), fDebug(false), fError(false),
feof(false), fReadShm(0){
  fpm = ETFParaManager::Instance();
} // end ctor

ETFUnpacker::~ETFUnpacker(){
  Close();
} // end dtor

// clear the output before each event loop
void ETFUnpacker::Reset(){}

// do some clean-ups
void ETFUnpacker::Close(){
  // fReadShm only non-nullptr in sim-daq mode
  if(fReadShm) delete fReadShm;
} // end member function Close

void ETFUnpacker::SetReadShm(ETFReadShm *r){
  if(!r) ETFMsg::Error("ETFUnpacker", "SetReadShm: nullptr encountered");
  fReadShm = r;
} // end member function SetReadShm

// Print the unpacked data of the current event
void ETFUnpacker::Print(){
  cout << "========= \033[1m" << (DaqType::kPXI == fType ? "PXI" : "VME");
  cout << "\033[0m Unpacker =========" << endl;
  cout << "-- ID: " << fId << " Event: " << fCurrentEventIndex;
  cout << " Event Count: " << fEventCnt << endl;
} // end member function Print
